<template>
    <div class="setting-container">
      <div class="setting-item setting-item-tab">
        <el-card style="background-color: #3C3F41;width: 100%;margin: 0 auto;border: 5px groove #b15b2e;padding:3px">
          <el-tabs>
            <el-tabs type="card" style="height: 100%; ">
          <el-tab-pane >
            <span slot="label" class="painting-setting-tabs-label ">涂胶设置</span>
            <div class="setting-item-tab-container">
              <el-card class="setting-item-tab-item painting-setting" style="background-color: #3C3F41;color: #fff;border: 5px ridge #E6A23C;padding:3px">
                <div slot="header">
                  <span>涂胶参数</span>
                </div>
                <el-form ref="form" :model="paintingForm" label-width="100px">

                  <el-form-item label="外圈取点数量" class="setting-item-label">
                    <el-input v-model="paintingForm.paintingParm.outsideContourPointNum" size="mini'"></el-input>
                  </el-form-item>
                  <el-form-item label="外圈涂胶距离" class="setting-item-label">
                    <el-input v-model="paintingForm.paintingParm.outsideContourPaintDistance" size="mini"></el-input>
                  </el-form-item>
                  <el-form-item label="外圈涂胶起点" class="setting-item-label">
                    <el-input v-model="paintingForm.paintingParm.outsideContourPaintStartPoint" size="mini"></el-input>
                  </el-form-item>
                  <el-form-item label="内圈取点数量" class="setting-item-label">
                    <el-input v-model="paintingForm.paintingParm.insideContourPointNum" size="mini"></el-input>
                  </el-form-item>
                  <el-form-item label="内圈涂胶距离" class="setting-item-label">
                    <el-input v-model="paintingForm.paintingParm.insideContourPaintDistance" size="mini"></el-input>
                  </el-form-item>
                </el-form>
              </el-card>
            <el-card class="setting-item-tab-item" style="background-color: #3C3F41;color: #fff;border: 5px ridge #E6A23C;padding:3px">
              <div slot="header">
                <span>外圈姿态轨迹</span>
              </div>
              <el-form ref="form" :model="paintingForm" label-width="160px">
                <el-form-item label="U轴默认值" class="setting-item-label">
                    <el-input v-model="paintingForm.outsidePoseTravel.uAxisDefault" size="mini"></el-input>
                  </el-form-item><el-form-item label="V轴默认值" class="setting-item-label">
                    <el-input v-model="paintingForm.outsidePoseTravel.vAxisDefault" size="mini"></el-input>
                  </el-form-item><el-form-item label="W轴默认值" class="setting-item-label">
                    <el-input v-model="paintingForm.outsidePoseTravel.wAxisDefault" size="mini"></el-input>
                  </el-form-item><el-form-item label="相机起始角度" class="setting-item-label">
                    <el-input v-model="paintingForm.outsidePoseTravel.cameraStartPosition" size="mini"></el-input>
                  </el-form-item><el-form-item label="机器人起始角度" class="setting-item-label">
                    <el-input v-model="paintingForm.outsidePoseTravel.axiosStartPosition" size="mini"></el-input>
                  </el-form-item><el-form-item label="机器人Z方向角度缩放" class="setting-item-label">
                    <el-input v-model="paintingForm.outsidePoseTravel.axiosZAxisScale" size="mini"></el-input>
                  </el-form-item><el-form-item label="机器人Z方向角度偏移量" class="setting-item-label">
                    <el-input v-model="paintingForm.outsidePoseTravel.axiosZAxisOffset" size="mini"></el-input>
                  </el-form-item>
              </el-form>
            </el-card>
            <div class="">
              <el-card class="setting-item-tab-item" style="background-color: #3C3F41;color: #fff;border: 5px ridge #E6A23C;padding:3px">
                 <div slot="header">
                <span>单点和内圈轨迹姿态</span>
              </div>
              <el-form ref="form" :model="paintingForm" label-width="100px">
                <el-form-item label="Z轴默认值" class="setting-item-label">
                    <el-input v-model="paintingForm.insidePoseTravel.zAxisDefault" size="mini"></el-input>
                  </el-form-item>
                <el-form-item label="U轴默认值" class="setting-item-label">
                    <el-input v-model="paintingForm.insidePoseTravel.uAxisDefault" size="mini"></el-input>
                  </el-form-item>
                <el-form-item label="V轴默认值" class="setting-item-label">
                    <el-input v-model="paintingForm.insidePoseTravel.vAxisDefault" size="mini"></el-input>
                  </el-form-item>
                <el-form-item label="W轴默认值" class="setting-item-label">
                    <el-input v-model="paintingForm.insidePoseTravel.wAxisDefault" size="mini"></el-input>
                  </el-form-item>
              </el-form>
            </el-card>
              <el-card class="setting-item-tab-item" style="background-color: #3C3F41;color: #fff;border: 5px ridge #E6A23C;padding:3px">
                <div slot="header">
                <span>胶枪设置</span>
              </div>
              <el-form ref="form" :model="paintingForm" label-width="100px">
                <el-form-item label="胶枪切换偏移" class="setting-item-label">
                    <el-input v-model="paintingForm.glueGunParm.glueGunChangeOffset" size="mini"></el-input>
                  </el-form-item>
                <el-form-item label="胶枪距离校准" class="setting-item-label">
                    <el-input v-model="paintingForm.glueGunParm.glueGunDistanceCalibrate" size="mini"></el-input>
                  </el-form-item>
              </el-form>
            </el-card>
            </div>
            </div>
            <el-card class="button-container" style="background-color: #3C3F41;color: #fff;border: 5px ridge #E6A23C;padding:3px">
              <el-button type="primary" size="medium" @click="paintingConfigReset">确认</el-button>
            </el-card>
          </el-tab-pane>
              <el-tab-pane>
                 <span slot="label" class="painting-setting-tabs-label ">标定设置</span>
                 <el-row :gutter="24">
                   <el-col :span="8">
                     <el-card style="background-color: #3C3F41;color: #fff;border: 5px ridge #E6A23C;padding:3px">
                       <el-image :src="imgUrl" fit="contain"></el-image>
                     </el-card>
                   </el-col>
                  <el-col :span="14">
                    <el-card style="background-color: #3C3F41;color: #fff;border: 5px ridge #E6A23C;padding:3px">
                      <div style="font-size: 14px; margin-top: 3px;padding: 0" slot="header">
                        图像坐标点
                         <el-button type="text" @click="readImgFile">读取文件</el-button>
                      </div>
                      <div>
                        <form>
                          <ul style="list-style-type: none">
                            <li>
                              <label for="i-p1">点1:</label>
                              <div style="display: inline-block" id="i-p1">
                                <input type="text" style="width: 60px;" v-model="imgPoints[0].x">
                                <input type="text"  style="width: 60px;" v-model="imgPoints[0].y">
                                <input type="text"  style="width: 60px;" v-model="imgPoints[0].z">
                              </div>
                            </li>
                            <li>
                              <label for="i-p2">点2:</label>
                              <div style="display: inline-block" id="i-p2">
                                <input type="text" style="width: 60px;" v-model="imgPoints[1].x">
                                <input type="text"  style="width: 60px;" v-model="imgPoints[1].y">
                                <input type="text"  style="width: 60px;" v-model="imgPoints[1].z">
                              </div>
                            </li>
                            <li>
                              <label for="i-p3">点3:</label>
                              <div style="display: inline-block" id="i-p3">
                                <input type="text" style="width: 60px;" v-model="imgPoints[2].x">
                                <input type="text"  style="width: 60px;" v-model="imgPoints[2].y">
                                <input type="text"  style="width: 60px;" v-model="imgPoints[2].z">
                              </div>
                            </li>
                            <li>
                              <label for="i-p4">点4:</label>
                              <div style="display: inline-block" id="i-p4">
                                <input type="text" style="width: 60px;" v-model="imgPoints[3].x">
                                <input type="text"  style="width: 60px;" v-model="imgPoints[3].y">
                                <input type="text"  style="width: 60px;" v-model="imgPoints[3].z">
                              </div>
                            </li>
                            <li>
                              <label for="i-p5">点5:</label>
                              <div style="display: inline-block" id="i-p5">
                                <input type="text" style="width: 60px;" v-model="imgPoints[4].x">
                                <input type="text"  style="width: 60px;" v-model="imgPoints[4].y">
                                <input type="text"  style="width: 60px;" v-model="imgPoints[4].z">
                              </div>
                            </li>
                          </ul>
                        </form>
                      </div>
                    </el-card>
                    <el-card style="background-color: #3C3F41;color: #fff;border: 5px ridge #E6A23C;padding:3px">
                      <div style="font-size: 14px; margin-top: 3px;padding: 0" slot="header">
                        机械手坐标点
                        <el-button type="text" @click="readAxiosFile">读取文件</el-button>
                      </div>
                      <div>
                        <form>
                          <ul style="list-style-type: none">
                            <li>
                              <label for="a-p1">点1:</label>
                              <div style="display: inline-block" id="a-p1">
                                <input type="text" style="width: 60px;" v-model="axiosPoints[0].x">
                                <input type="text"  style="width: 60px;" v-model="axiosPoints[0].y">
                                <input type="text"  style="width: 60px;" v-model="axiosPoints[0].z">
                              </div>
                            </li>
                            <li>
                              <label for="a-p2">点2:</label>
                              <div style="display: inline-block" id="a-p2">
                                <input type="text" style="width: 60px;" v-model="axiosPoints[1].x">
                                <input type="text"  style="width: 60px;" v-model="axiosPoints[1].y">
                                <input type="text"  style="width: 60px;" v-model="axiosPoints[1].z">
                              </div>
                            </li>
                            <li>
                              <label for="a-p3">点3:</label>
                              <div style="display: inline-block" id="a-p3">
                                <input type="text" style="width: 60px;" v-model="axiosPoints[2].x">
                                <input type="text"  style="width: 60px;" v-model="axiosPoints[2].y">
                                <input type="text"  style="width: 60px;" v-model="axiosPoints[2].z">
                              </div>
                            </li>
                            <li>
                              <label for="a-p4">点4:</label>
                              <div style="display: inline-block" id="a-p4">
                                <input type="text" style="width: 60px;" v-model="axiosPoints[3].x">
                                <input type="text"  style="width: 60px;" v-model="axiosPoints[3].y">
                                <input type="text"  style="width: 60px;" v-model="axiosPoints[3].z">
                              </div>
                            </li>
                            <li>
                              <label for="a-p5">点5:</label>
                              <div style="display: inline-block" id="a-p5">
                                <input type="text" style="width: 60px;" v-model="axiosPoints[4].x">
                                <input type="text"  style="width: 60px;" v-model="axiosPoints[4].y">
                                <input type="text"  style="width: 60px;" v-model="axiosPoints[4].z">
                              </div>
                            </li>
                          </ul>
                        </form>
                      </div>
                    </el-card>
                  </el-col>
                 </el-row>
                <el-card style="background-color: #3C3F41;color: #fff;border: 5px ridge #E6A23C;padding:3px; margin: 20px auto; width: 80%;">
                  <el-button type="primary" @click="unitCalibrate" v-loading="createFileLoading">点击扫描图像</el-button>
                  <el-button type="primary" @click="createOffset" v-loading="createFileLoading">点击生成参数</el-button>
                  <el-button type="primary" @click="createImgPointFile" v-loading="createFileLoading">点击生成图像点位参数(用于测试)</el-button>

                </el-card>
              </el-tab-pane>
        </el-tabs>
          </el-tabs>
        </el-card>
      </div>
    </div>
</template>

<script>
  import axios from 'axios'
  import {mapState, mapMutations} from 'vuex'
    export default {
        name: "SettingContainer",
        computed: {
          ...mapState([
            'imgCalibration',
            'ws',
            'createFileLoading',
            'showText',
          ])
        },
        mounted() {

        },
      data(){

          return{
            paintingForm: {
              'paintingParm': {
                'outsideContourPointNum': 0 , //外圈取点数量
                'outsideContourPaintDistance': 0, //外圈涂胶距离
                'outsideContourPaintStartPoint': 0, //外圈涂胶起点
                'insideContourPointNum': 0, //内圈取点数量
                'insideContourPaintDistance': 0, //内圈涂胶距离
              },
              'outsidePoseTravel': {
                'uAxisDefault': 0, //U轴默认值
                'vAxisDefault': 0, //V轴默认值
                'wAxisDefault': 0, //W轴默认值
                'cameraStartPosition': 0, //相机起始角度
                'axiosStartPosition': 0, //机器人起始角度
                'axiosZAxisScale': 0, //机器人Z方向角度缩放
                'axiosZAxisOffset': 0, //机器人Z方向偏移量
              },
              'insidePoseTravel':{
                'zAxisDefault': 0, //Z轴默认值
                'uAxisDefault': 0, //U轴默认值
                'vAxisDefault': 0, //V轴默认值
                'wAxisDefault': 0, //W轴默认值
              },
              'glueGunParm':{
                'glueGunChangeOffset': 0, //胶枪切换偏移
                'glueGunDistanceCalibrate': 0, //胶枪距离校准
              }
            },
            imgPoints : [
              {'x':0, 'y':0, 'z': 0},
              {'x':0, 'y':0, 'z': 0},
              {'x':0, 'y':0, 'z': 0},
              {'x':0, 'y':0, 'z': 0},
              {'x':0, 'y':0, 'z': 0},
            ],
            axiosPoints : [
              {'x':0, 'y':0, 'z': 0},
              {'x':0, 'y':0, 'z': 0},
              {'x':0, 'y':0, 'z': 0},
              {'x':0, 'y':0, 'z': 0},
              {'x':0, 'y':0, 'z': 0},
            ],
            imgUrl: "http://127.0.0.1:8000/media/img/calibration.jpg",
          }
        },
      methods: {
        ...mapMutations([
            "activeCalibrateState", "sleepCalibrateState"
          ]),
        paintingConfigReset(){
            axios
              .post('http://127.0.0.1:8000/test/set_paint_config', {
                config: JSON.stringify(this.paintingForm)
              })
              .then(res => {
                console.log(JSON.parse(res.data.config))
              })
              .catch(err => {
                console.log(err)
              })
          },
        createOffset(){
            this.activeCalibrateState()
            axios
              .post('http://127.0.0.1:8000/test/compute_img_axios_offset', {
                axios_points: JSON.stringify(this.axiosPoints)
              })
              .then(res => {
                if(res.data){
                  this.$message({
                    message: '生成成功',
                    type: 'success',
                  })
                }else{
                  this.$message.error("生成失败")
                }
              });
            this.sleepCalibrateState()
        },
        createImgPointFile(){
            this.activeCalibrateState()
            axios
              .get('http://127.0.0.1:8000/test/crate_calibrate_file')
              .then(res => {
                if(res.data){
                  this.$message({
                    message: '生成成功',
                    type: 'success',
                  });
                    this.sleepCalibrateState()
                  this.readImgFile()
                }else{
                  this.$message.error("生成失败");
                  this.sleepCalibrateState()
                }
              })
        },
        unitCalibrate(){
           this.activeCalibrateState()
           this.ws.send("calibrate")
            setTimeout(()=>{
              if (this.showText[0] === ""){
                this.$message.error("扫描失败")
              }else{
                this.readImgFile()
                this.$message.success("生成成功")
              }
              this.sleepCalibrateState()
            }, 5000)
        },
        readImgFile(){
            this.ws.send("getImgCalibratePoint")
            setTimeout(() =>{
               this.imgPoints = JSON.parse(this.imgCalibration)
            }, 50)
        },
        readAxiosFile(){
            axios
              .get('http://127.0.0.1:8000/test/get_axios_file')
              .then((res)=>{
                this.axiosPoints = res.data.points
              })
        }
      },
    }
</script>

<style scoped>
.button-container {

}
.setting-container {
  display: flex;
  justify-items: flex-start;
  padding: 20px;
   background-color: #2B2B2B;
    width: 100% ;
}
.setting-item {
  margin: 0 auto;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  border: 1px solid #d7dae2;
}
  .setting-item-tab {
    width: 1500px;
  }
  .setting-item-tab-container{
    display: flex;
    justify-items: flex-start;
  }
  .setting-item-tab-item {
    width: 350px;
    margin: 30px
  }
  .painting-setting {

  }
.painting-setting-tabs-label {
  color: #9E9F9F
}
.painting-setting-tabs-label:hover {
  color: #b15b2e;
}
</style>

